/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectData;
import DTO.Comment;
import DTO.Product;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Vien-IT
 */
public class BComment {
    private ConnectData condata ;

    public BComment() {
        condata = new ConnectData();
    }
    
    public ArrayList<Comment> getAll(String sql)
    {
        ArrayList<Comment> comments = new ArrayList<Comment>();
        Comment comment = null;
        try {
            ResultSet rs = condata.executeQuery(sql);
            while(rs.next())
            {
                comment = new Comment();
                comment.setComment(rs.getString("comment"));
                comment.setUserId(rs.getInt("userId"));
                comment.setProductId(rs.getInt("productId"));
                comments.add(comment);
               
            }
             condata.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        
        return comments.isEmpty()? null : comments;
    }
    
    
   public ArrayList<Comment> getAll()
   {
       String sql = "select * from tblProduct";
       return this.getAll(sql);
   }
   
   public Comment getById(int productId)
   {
       String sql= "select * from tblProduct where productId = "+productId+"";
       ArrayList<Comment> comments = getAll(sql);
       return comments == null ? null : comments.get(0);
   }
   
   public ArrayList<Comment> getByUserId(int userId)
   {
       String sql = "select * from tblComment where userId = "+userId+"";
       return this.getAll(sql);
   }
   
   public ArrayList<Comment> getByProductId(int productId)
   {
       String sql= "select * from tblComment where product = "+productId+"";
       return this.getAll(sql);
   }
   
   public boolean insert(Comment comment)
   {
       boolean check = false;
       int rs = -1;
       String sql = "insert into tblComment(comment,userId,productId) "
               + "values (N'"+comment.getComment()+"', "+comment.getUserId()+","+comment.getProductId()+")";
       try {
           rs = condata.executeUpdate(sql);
           if(rs > 0)
               check = true;
       } catch (Exception e) {
       }
       return check;
   }
    
}
